<?php
/***
*
* Class: OrderValidator //订单验证器
* Author: Jason ZHENG
* Data: 2018-07-04
*
***/
namespace App\Validator;

use App\Service\DataReturnService as DRS;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session; 
use App\Common\Base;

class OrderValidator
{
	// 验证并返回产品信息
    public static function validateGetProducts($product_ids, $delivery_id, $user_id)
    {

        $product_infos = [];
        foreach ($product_ids as $product_id) {
            $product =  DB::table('product')->where( ['id'=>$product_id, 'state' => 0] )->first();
            if(empty($product))
                return DRS::returnError(1000,'商品不存在！',$product);
            elseif($product->is_onsale == 0 )
                return DRS::returnError(1002,$product->full_name.'已下架！',$product);
            elseif($product->stock < 1 )
                return DRS::returnError(1003,$product->full_name.'库存不足！',$product);
            elseif($product->is_own != 0 )
                return DRS::returnError(1004,$product->full_name.'非本商户商品！',$product);
            elseif($product->product_state != 1 && $product->product_state != 2 && $product->product_state != 3 && $product->product_state != 4 )
                return DRS::returnError(1004,$product->full_name.'不在可售状态！',$product);

            $order_info =  DB::table('orders_info')->where( ['product_id'=>$product_id, 'state' => 0, 'status' => ['in',[1,2,3,4,5,6]]] )->first();
            if(!empty($order_info))
            {
                if($order_info->user_id == $user_id){
                    return DRS::returnError(1005,'该商品已经存在自己订单中！',$order_info);
                }else{
                    return DRS::returnError(1006,'该商品已经存在别人订单中！',$order_info);
                }
            }
            $product_infos [] = (array) $product;
        }

        if($delivery_id == '' || $delivery_id == null || $delivery_id == 0){
            return DRS::returnError(1101,'未提供收货地址！');
        }else{
            $delivery = DB::table('delivery')->where(['id' => $delivery_id])->first();
            if(empty($delivery)){
                return DRS::returnError(1102,'找不到该收货地址！',$delivery);
            }elseif($delivery->user_id != $user_id){
                return DRS::returnError(1102,'该收货地址不是该账户下的地址！',$delivery);
            }elseif($delivery->state != 0){
                return DRS::returnError(1102,'该收货地址已禁用！',$delivery);
            }
        }

        return DRS::returnOk($product_infos);
    }

    // 验证并返回优惠券信息
    public static function validateGetTicket($ticket_id, $total_price, $user_id)
    {
        $ticket = DB::table('tickets')->where(['id' => $ticket_id])->first();
        if(empty($ticket)){
           return DRS::returnError(1100,'该优惠券不存在！'); 
        }elseif($ticket->start_date > date('Y-m-d H:i:s')){
            return DRS::returnError(1101,'该优惠券还没到起效时间！');
        }elseif($ticket->expire_date < date('Y-m-d H:i:s')){
            return DRS::returnError(1102,'该优惠券已过失效时间！');
        }elseif($ticket->state != 0){
            return DRS::returnError(1103,'该优惠券已禁用！');
        }elseif($ticket->total > $total_price){
            return DRS::returnError(1104,'您购买的商品总额未达到优惠券使用条件！');
        }else{
            $ticket_user = DB::table('ticket_user')->where(['ticket_id' => $ticket_id, 'user_id' => $user_id])->first();
            if(empty($ticket_user)){
                return DRS::returnError(1105,'您还未领取该优惠券！');
            }elseif($ticket_user->state != 0){
                return DRS::returnError(1106,'您已用过该优惠券！');
            }else{
                return DRS::returnOk((array)$ticket);
            }
        }
        
    }

    //验证并获取订单
    public static function validateGetOrder($batchcode, $user_id)
    {
        $select = ['o.id','o.business_id','o.user_id','o.state','o.batchcode','o.uniqueOrderNo','o.total_price','o.total_fare','o.coupon_id','t.total as coupon_total', 'o.transaction_id', 'o.pay_time', 'o.shipping_sn',
                    't.price as coupon_price','t.start_date as coupon_start_date','t.expire_date as coupon_expire_date',
                    'o.payment_status','o.status','sc.value as status_value','o.createtime','o.remark','o.pay_code','o.delivery_id','d.province','d.city',
                    'd.district','d.address','d.postalcode','d.phone','d.name'];
        $order = DB::table('orders as o')
                ->where(['o.batchcode' => $batchcode])
                ->leftJoin('delivery as d','d.id','=','o.delivery_id')
                ->leftJoin('tickets as t','t.id','=','o.coupon_id')
                ->leftJoin('sales_condition as sc','sc.id','=','o.status')
                ->select($select)
                ->first();
        if($order == null)
            return DRS::returnError(1001,'未找到订单！');
        
        if($order->user_id != $user_id)
            return DRS::returnError(1001,'非该用户的订单！');

        if($order->state == 1)
            return DRS::returnError(1001,'该订单已删除！');

        return DRS::returnOk((array)$order);

    }

    //验证并获取订单的产品详情
    public static function validateGetOrderInfo($batchcode, $user_id)
    {
        $select = ['oi.id','oi.user_id','oi.state','oi.batchcode','oi.info_batchcode','oi.product_id','oi.coupon_id','t.total as coupon_total','oi.shipping_sn','oi.shipping_code','oi.pay_batchode',
                    't.price as coupon_price','t.price as coupon_price','t.start_date as coupon_start_date','t.expire_date as coupon_expire_date',
                    't.picture as coupon_picture','oi.price','p.product_name','p.full_name',
                    'p.category_id','p.type_id','p.brand_id','p.systematics_id','p.is_shelf','w.value as shelf_value','p.is_onsale','p.is_new','p.is_hot',
                    'p.pic_url','p.is_recommend','p.product_sn','p.standard_id','p.price','p.cost_price',
                    'p.peer_price','p.activity_price','p.remark','p.product_cate','p.product_condition','pf.value as product_condition_value',
                    'p.procurement_status','ps.value as procurement_value',
                    'p.product_code','p.coordinate','p.library_age','p.additional','p.product_source','p.synopsis','p.is_own','p.product_state'];
        $ordersInfo = DB::table('orders_info as oi')
                ->where(['oi.batchcode' => $batchcode])
                ->leftJoin('product as p','p.id','=','oi.product_id')
                ->leftJoin('tickets as t','t.id','=','oi.coupon_id')
                ->leftJoin('warehouse_condition as w','w.id','=','p.is_shelf')
                ->leftJoin('product_fineness as pf','pf.id','=','p.product_condition')
                ->leftJoin('perchase_source as ps','ps.id','=','p.procurement_status')
                ->select($select)
                ->get()
                ->toArray();
        if($ordersInfo == null)
            return DRS::returnError(1001,'未找到订单！');
        
        foreach ($ordersInfo as $orderInfo) {
            if($orderInfo->user_id != $user_id)
                return DRS::returnError(1001,'非该用户的订单！');
            if($orderInfo->state == 1)
                return DRS::returnError(1001,'该订单已删除！');
        }
    
        return DRS::returnOk(array_map('get_object_vars', $ordersInfo));

    }

    //验证取消订单的订单信息
    public static function validateRemoveOrder($batchcode, $user_id)
    {
        $order = DB::table('orders')
                ->where(['batchcode' => $batchcode])
                ->first();
        if($order == null)
            return DRS::returnError(1001,'未找到订单！');
        
        if($order->user_id != $user_id)
            return DRS::returnError(1001,'非该用户的订单！');

        if($order->state == 1)
            return DRS::returnError(1001,'该订单已删除！');

        if($order->status != 1)
        	return DRS::returnError(1001,'不能取消该订单！');

        return DRS::returnOk((array)$order);

    }

    //验证取消订单的订单详情信息
    public static function validateRemoveOrderInfo($batchcode, $user_id)
    {
        $select = ['oi.id','oi.user_id','oi.state','oi.batchcode','oi.product_id','oi.coupon_id', 'oi.status' ,'t.total as coupon_total',
                    't.price as coupon_price','t.price as coupon_price','t.start_date as coupon_start_date','t.expire_date as coupon_expire_date',
                    't.picture as coupon_picture','oi.price','p.product_name','p.full_name',
                    'p.category_id','p.type_id','p.brand_id','p.systematics_id','p.is_shelf','w.value as shelf_value','p.is_onsale','p.is_new','p.is_hot',
                    'p.pic_url','p.is_recommend','p.product_sn','p.standard_id','p.price','p.cost_price',
                    'p.peer_price','p.activity_price','p.remark','p.product_cate','p.product_condition','pf.value as product_condition_value',
                    'p.procurement_status','ps.value as procurement_value',
                    'p.product_code','p.coordinate','p.library_age','p.additional','p.product_source','p.synopsis','p.is_own','p.product_state','p.is_synchro','p.source_product_id', 'p.is_autodeliver' ,'p.is_sold_business'];
        $ordersInfo = DB::table('orders_info as oi')
                ->where(['oi.batchcode' => $batchcode])
                ->leftJoin('product as p','p.id','=','oi.product_id')
                ->leftJoin('tickets as t','t.id','=','oi.coupon_id')
                ->leftJoin('warehouse_condition as w','w.id','=','p.is_shelf')
                ->leftJoin('product_fineness as pf','pf.id','=','p.product_condition')
                ->leftJoin('perchase_source as ps','ps.id','=','p.procurement_status')
                ->select($select)
                ->get()
                ->toArray();
        if($ordersInfo == null)
            return DRS::returnError(1002,'未找到订单里的商品！');
        
        foreach ($ordersInfo as $orderInfo) {
            if($orderInfo->user_id != $user_id)
                return DRS::returnError(1002,'非该用户的订单商品！');
            if($orderInfo->state == 1)
                return DRS::returnError(1002,'该订单已删除！');
            if($orderInfo->status != 1)
            	return DRS::returnError(1002,'不能取消该订单！');
            // if(!in_array($orderInfo->product_state, [5,6,7,8]))
            // 	return DRS::returnError(1002,'不能取消该订单！');
        }
        return DRS::returnOk(array_map('get_object_vars', $ordersInfo));
    }

    //验证确认收货的订单信息
    public static function validateConfirmOrder($batchcode, $user_id)
    {
        $order = DB::table('orders')
                ->where(['batchcode' => $batchcode])
                ->first();
        if($order == null)
            return DRS::returnError(1001,'未找到订单！');
        
        if($order->user_id != $user_id)
            return DRS::returnError(1001,'非该用户的订单！');

        if($order->state == 1)
            return DRS::returnError(1001,'该订单已删除！');

        if($order->status != 3)
            return DRS::returnError(1001,'该订单不能确认收货！');

        return DRS::returnOk((array)$order);

    }

    //验证确认收货的订单详情信息
    public static function validateConfirmOrderInfo($batchcode, $user_id)
    {
        $select = ['oi.id','oi.user_id','oi.state','oi.batchcode','oi.product_id','oi.coupon_id', 'oi.status' ,'t.total as coupon_total',
                    't.price as coupon_price','t.price as coupon_price','t.start_date as coupon_start_date','t.expire_date as coupon_expire_date',
                    't.picture as coupon_picture','oi.price','p.product_name','p.full_name',
                    'p.category_id','p.type_id','p.brand_id','p.systematics_id','p.is_shelf','w.value as shelf_value','p.is_onsale','p.is_new','p.is_hot',
                    'p.pic_url','p.is_recommend','p.product_sn','p.standard_id','p.price','p.cost_price',
                    'p.peer_price','p.activity_price','p.remark','p.product_cate','p.product_condition','pf.value as product_condition_value',
                    'p.procurement_status','ps.value as procurement_value',
                    'p.product_code','p.coordinate','p.library_age','p.additional','p.product_source','p.synopsis','p.is_own','p.product_state'];
        $ordersInfo = DB::table('orders_info as oi')
                ->where(['oi.batchcode' => $batchcode])
                ->leftJoin('product as p','p.id','=','oi.product_id')
                ->leftJoin('tickets as t','t.id','=','oi.coupon_id')
                ->leftJoin('warehouse_condition as w','w.id','=','p.is_shelf')
                ->leftJoin('product_fineness as pf','pf.id','=','p.product_condition')
                ->leftJoin('perchase_source as ps','ps.id','=','p.procurement_status')
                ->select($select)
                ->get()
                ->toArray();
        if($ordersInfo == null)
            return DRS::returnError(1002,'未找到订单里的商品！');
        
        foreach ($ordersInfo as $orderInfo) {
            if($orderInfo->user_id != $user_id)
                return DRS::returnError(1002,'非该用户的订单商品！');
            if($orderInfo->state == 1)
                return DRS::returnError(1002,'该订单已删除！');
            if($orderInfo->status != 3)
                return DRS::returnError(1002,'该订单不能确认收货！');
            // if($orderInfo->product_state != 13)
            //     return DRS::returnError(1002,'该商品不能确认收货！');
        }
        return DRS::returnOk(array_map('get_object_vars', $ordersInfo));
    }

}